package artaza.clasesdao;
import artaza.persistencia.*;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class GenericDAOJPAImpl<T, PK extends Serializable> implements GenericDAO<T, PK> {

    @PersistenceContext
    EntityManager em;

    @Override
    public void insert(T t) {
        em.persist(t);
    }

    @Override
    public T findByPK(Class<T> typeClass, PK id) {
        return em.find(typeClass, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<T> getAll(Class<T> typeClass) {

        final StringBuilder sql = new StringBuilder("select c from ").append(
                typeClass.getSimpleName()).append(" c");
        return (List<T>) em.createQuery(sql.toString()).getResultList();

    }

    @Override
    public void remove(T object) {

        em.remove(em.merge(object));
    }

    @Override
    public GenericDAO<T, PK> update(T object) {
        em.merge(object);
        return this;
    }

    @Override
    public T findByDNI(Class<T> typeClass, String criterio) {
        String query = "SELECT FROM Camarero c WHERE c.dni='" + criterio + "'";
        Camarero c = (Camarero) em.createQuery(query).getSingleResult();
        return em.find(typeClass, c.getId());
    }

    @Override
    public List<T> findByNombre(Class<T> typeClass, String criterio) {
        String query = "SELECT FROM Camarero c WHERE c.nombre='" + criterio + "'";
        List<T> camareros = (List<T>) em.createQuery(query).getResultList();
        return camareros;
    }

    @Override
    public  List<T> findByApellidos(Class<T> typeClass, String criterio) {
        String query = "SELECT FROM Camarero c WHERE c.apellidos='" + criterio + "'";
         List<T> camareros = (List<T>) em.createQuery(query).getResultList();
        return camareros;
    }

    @Override
    public  List<T> findByTelefono(Class<T> typeClass, String criterio) {
        String query = "SELECT FROM Camarero c WHERE c.telefono='" + criterio + "'";
         List<T> camareros = (List<T>) em.createQuery(query).getResultList();
        return camareros;
    }
    @Override
    public  List<T> findByCategoria(Class<T> typeClass, String criterio) {
        String query = "SELECT FROM Camarero c WHERE c.categoria='" + criterio + "'";
        List<T> camareros = (List<T>) em.createQuery(query).getResultList();
        return camareros;
    }
    
    @Override
    public T findByNombref(Class<T> typeClass, String criterio) {
        String query = "SELECT FROM Finca f WHERE f.nombre='" + criterio + "'";
        Finca finca =  (Finca) em.createQuery(query).getSingleResult();
        return (T) finca;
    }

    @Override
    public int removeAll(Class<T> typeClass) {
        String sql = "DELETE FROM " + typeClass.getSimpleName() + " c";
        return em.createQuery(sql).executeUpdate();
    }

    @Override
    public Integer countAll(Class<T> typeClass) {
        final StringBuilder sql = new StringBuilder("select count(c) from ").append(
                typeClass.getSimpleName()).append(" c");
        return (Integer) em.createQuery(sql.toString()).getSingleResult();
    }

    @Override
    public T findByNombreR(Class<T> typeClass, String criterio) {
        String query = "SELECT FROM Rol r WHERE r.nombre='" + criterio + "'";
        Rol r =  (Rol) em.createQuery(query).getSingleResult();
        return (T) r;
    }
}
